<fieldset>
	<legend> Forums </legend>
		<br/>
			<div id="champs">
				<?php
					if (isset($message)) {
						echo "<p>";
						echo $message;
						echo "</p>";
					} else {				
						$max = count($tab['id_categ']);
						$count = 0;
						for($i=0 ; $i < $max ; $i++) {
							if (isset($_SESSION['id_user'])) {
								if(($_SESSION['id_user'] == $tab['id_user'][$i]) || (isset($_SESSION['droit']) && $_SESSION['droit'] != 0 )) {
									$count++;
									if ($count == 1) {
										if (isset($_GET['archive'])) {
											echo "<TABLE BORDER=","1",">
													<CAPTION> Liste des archives : </CAPTION>
													<TR>
														<TH> Index </TH>
														<TH> Redacteur </TH>
														<TH> Categorie </TH>
														<TH> Texte </TH>
														<TH> Date publication </TH>
														<TH> Nombre reponse </TH>
														<TH> Action </TH>
														
													</TR>";
										}
										else {
											echo "<TABLE BORDER=","1",">
													<CAPTION> Liste des forums : </CAPTION>
													<TR>
														<TH> Index </TH>
														<TH> Redacteur </TH>
														<TH> Categorie </TH>
														<TH> Texte </TH>
														<TH> Date publication </TH>
														<TH> Nombre reponse </TH>
														<TH> Action </TH>
														
													</TR>";
										}
									}
									echo "<TR>";
									echo "<TD>", $tab['id_forum'][$i], "</TD>";
									require_once("model/userBD.php");
									$user=select_user_by_id($tab['id_user'][$i]);
									echo "<TD>", $user['nom'][0], " ", $user['prenom'][0], "</TD>";
									require_once("model/categoryBD.php");
									$categ=select_category_by_id($tab['id_categ'][$i]);
									echo "<TD>", $categ['name_categ'][0], "</TD>";
									echo "<TD>", $tab['text_forum'][$i], "</TD>";
									echo "<TD>", $tab['date_forum'][$i], "</TD>";
									$id = $tab['id_forum'][$i];
									require_once("model/answerBD.php");
									$nb_answer=select_nb_answer_forum($id);
									echo "<TD>", $nb_answer['nb_rep'], "</TD>";
									echo "<TD>";
									echo "<a href=\"index.php?page=view_forum&id_forum=$id\">";
									echo "<img src=\"./images/action/loupe.png\" title=\"Voir le forum\"/>";
									echo "</a>";
									if(($_SESSION['id_user'] == $tab['id_user'][$i]) || (isset($_SESSION['droit']) && ($_SESSION['droit'] == 1 || $_SESSION['droit'] == 6))) {
									echo "<a href=\"index.php?page=edit_forum&id_forum=$id\" onClick=\"affiche_cache('update_categ')\">";
									echo "<img src=\"./images/action/feuille.png\" title=\"Modifier le forum\"/>";
									echo "</a>";
									if (!isset($_GET['archive'])) {
										echo "<a href=\"index.php?page=archive_forum&id_forum=$id\">";
										echo "<img src=\"./images/action/archiver.png\" title=\"Archiver le forum\"/>";
										echo "</a>";
									}
									echo "<a href=\"index.php?page=delete_forum&id_forum=$id\">";
									echo "<img src=\"./images/action/croix.png\" title=\"Supprimer le forum\"/>";
									echo "</a>";
									}
									echo "</TD>";
									echo "</TR>";
								}
							}
						}
						if ($count == 0)
							echo "<h3> Vous n'avez publier aucun forum </h3>";
						else
							echo "</TABLE>";
					}
							
					if ($_SESSION['droit'] == 0) {
						require_once("model/subscriptionBD.php");
						require_once("model/forumBD.php");
						$subscriber = view_simple_subscriptionBD();
						$maxsub = count($subscriber['id_user']);
						$countabo = 0;
						for ($i=0 ; $i < $maxsub ; $i++) {
							$countabo++;
							if ($countabo == 1) {
								echo "<TABLE BORDER=","1",">
										<CAPTION> Liste des forums des abonnes : </CAPTION>
										<TR>
											<TH> Index </TH>
											<TH> Redacteur </TH>
											<TH> Categorie </TH>
											<TH> Texte </TH>
											<TH> Date publication </TH>
											<TH> Nombre reponse </TH>
											<TH> Action </TH>
										</TR>";
							}
							echo "<TR>";
							$forums = select_forum_by_iduser($subscriber['id_subscriber'][$i]);
							$maxforum = count($forums['id_forum']); 
							for ($j=0 ; $j < $maxforum ; $j++) {
								echo "<TD>", $forums['id_forum'][$j], "</TD>";
								require_once("model/userBD.php");
								$user=select_user_by_id($forums['id_user'][$j]);
								echo "<TD>", $user['nom'][0], " ", $user['prenom'][0], "</TD>";
								require_once("model/categoryBD.php");
								$categ=select_category_by_id($forums['id_categ'][$j]);
								echo "<TD>", $categ['name_categ'][0], "</TD>";
								echo "<TD>", $forums['text_forum'][$j], "</TD>";
								echo "<TD>", $forums['date_forum'][$j], "</TD>";
								$id = $forums['id_forum'][$j];
								require_once("model/answerBD.php");
								$nb_answer=select_nb_answer_forum($id);
								echo "<TD>", $nb_answer['nb_rep'], "</TD>";
								echo "<TD>";
								echo "<a href=\"index.php?page=view_forum&id_forum=$id\">";
								echo "<img src=\"./images/action/loupe.png\" title=\"Voir le forum de l'abonne\"/>";
								echo "</a>";
								echo "</TD>";
								echo "</TR>";
							}
						}
						if ($countabo == 0)
							echo "<h3> Vos abonnes n'ont publie aucun forum </h3>";
						else
							echo "</TABLE>";
					}

					
				?>
				<?php
					if (!isset($_GET['archive'])) {
						echo "<div id=\"zoom_forum\">";
						echo "<a href=\"index.php?page=archive&\"><h2>Voir les archives</h2></a>";
						echo "</div>";
						if (isset($_SESSION['id_user']))
							echo "<div id=","add_comments",">
								<p><input type=","submit"," name=","bt_add_comment"," value=\"Ajouter un forum\" /> </p> <br/>";
							echo "</div>";
					}
				?>
				<div class="add_comment">
					</br>
					</br>
					<form method="post" action="index.php?page=add_forum" onSubmit="return verifFormforum(this)">
						<fieldset>
							<div id="champs_forum">
								<label for="name_categ"> Categorie : </label>
								<?php
									echo "<select name=\"name_categ\">";
									require_once("model/categoryBD.php");
									$lstcateg=select_category();
									$max = count($lstcateg['id_categ']);
									for($i=0 ; $i < $max ; $i++) {
										echo "<option>", $lstcateg['name_categ'][$i] ,"</option>";
									}
									echo "</select><br/>";
								?>
								<label for="text_forum"> Texte : </label>
								<textarea cols="30" rows="8" name="text_forum">Votre texte</textarea>
							</div> <!-- fin div champs_categ -->
						</fieldset>
						<p style="text-align:right"><input type="submit" value="Publier"/></p>
					</form>
				</div> <!-- fin div add_comment -->
				
				<div id="update_forum" style="display: <?php if (isset($display)) echo $display; else echo "none"; ?> ">
					</br>
					</br>
					<form method="post" action="index.php?page=update_forum" onSubmit="return verifFormforum(this)">
						<fieldset>
							<div id="champs_forum">
								<input name="id_forum" value="<?php echo $tab['id_forum'][0]; ?>" style="display: none"/>
								<label for="name_categ"> Categorie : </label>
								<?php
									if (!isset($message)) {
										echo "<select name=\"name_categ\">";
										require_once("model/categoryBD.php");
										$categ=select_category_by_id($tab['id_categ'][0]);
										echo "<option>", $categ['name_categ'][0], "</option>";
										$lstcateg=select_category();
										$max = count($lstcateg['id_categ']);
										for($i=0 ; $i < $max ; $i++) {
											if($lstcateg['name_categ'][$i] != $categ['name_categ'][0])
												echo "<option>", $lstcateg['name_categ'][$i] ,"</option>";
										}
										echo "</select><br/>";
									}
								?>
								<label for="text_forum"> Texte : </label>
								<textarea cols="30" rows="8" name="text_forum" <?php if (isset($no_modif)) echo "readonly=", $no_modif, "\""; ?>><?php echo $tab['text_forum'][0]; ?></textarea>
							</div> <!-- fin div champs_forum -->
						</fieldset>
						<p style="text-align:right"><input type="submit" value="Mettre a jour" <?php if (isset($no_modif)) echo "style=\"display: none\""; ?>/></p><br/>
					</form>
				</div> <!-- fin div update_categ -->
				<?php if (isset($_GET['id_forum'])) {
					echo "<div id=\"zoom_forum\">";
					 echo "<a href=\"#inline-1\" rel=\"prettyPhoto\"><h2>Voir le forum selectionne</h2></a>";
					echo "<div id=\"inline-1\">";
						$id_forum=$_GET['id_forum'];
						require_once("model/userBD.php");
						$user=select_user_by_id($tab['id_user'][0]);
						$date=explode('-',$tab['date_forum'][0]);
						echo "Forum publie par ", $user['civilite'][0], " ", $user['nom'][0], " ", $user['prenom'][0]," le ", $date[2],"/",$date[1],"/",$date[0];
						$user_civilite = $user['civilite'][0];
						$user_nom = $user['nom'][0];
						$user_prenom = $user['prenom'][0];
						$date0 = $date[0];
						$date1 = $date[1];
						$date2 = $date[2];
						$forum_publie = "Forum publie par $user_civilite $user_nom $user_prenom le $date2/$date1/$date0\r\n";
						echo "<br/>";
						echo "Categorie : ";
						require_once("model/categoryBD.php");
						$categ=select_category_by_id($tab['id_categ'][0]);
						echo $categ['name_categ'][0];
						$categorie = $categ['name_categ'][0];
						$forum_categorie = "Categorie : $categorie\r\n";
						echo "<br/>";
						echo "Texte : ";
						echo $tab['text_forum'][0];
						$texte = $tab['text_forum'][0];
						$forum_texte = "Texte : $texte\r\n\r\n";
						echo "<br/>";
						echo "<br/>";
						require_once("model/answerBD.php");
						$answer=select_answer_forum($_GET['id_forum']);
						$nom_fichier = $tab['text_forum'][0];
						$fichier = "./download/$nom_fichier.txt";
						$fichier_a_ouvrir = fopen ($fichier, "w+");
						fwrite($fichier_a_ouvrir,$forum_publie);
						fwrite($fichier_a_ouvrir,$forum_categorie);
						fwrite($fichier_a_ouvrir,$forum_texte);
						for($i=0 ; $i < count($answer['id_answer']) ; $i++) {
							require_once("model/userBD.php");
							$user_answer=select_user_by_id($answer['id_user'][$i]);
							$date = explode('-', $answer['date_answer'][$i]);
							echo "Reponse publie par ", $user_answer['civilite'][0], " ", $user_answer['nom'][0], " ", $user_answer['prenom'][0]," le ", $date[2],"/",$date[1],"/",$date[0], "</a>";
							echo "<br/>";
							echo $answer['text_answer'][$i];
							echo "<br/><br/>";
							$answer_civilite = $user_answer['civilite'][0];
							$answer_nom = $user_answer['nom'][0];
							$answer_prenom = $user_answer['prenom'][0];
							$answer_date0 = $date[0];
							$answer_date1 = $date[1];
							$answer_date2 = $date[2];
							$reponse = $answer['text_answer'][$i];
							$forum_reponse = "Reponse publie par $answer_civilite $answer_nom $answer_prenom le $answer_date2/$answer_date1/$answer_date0\r\n$reponse\r\n\r\n";
							fwrite($fichier_a_ouvrir,$forum_reponse);
						}
						fclose ($fichier_a_ouvrir);
						echo "<form method=\"post\" action=\"index.php?page=add_answer&id_forum=$id_forum\" onSubmit=\"\">";
						echo "<label for=\"text_answer\"> Repondre : </label>
								<input name=\"text_answer\"/><br/>";
						echo "<p style=\"text-align:right\"><input type=\"submit\" value=\"Envoyer\"/></p>";
						echo "</form>";
						echo "<a href=\"$fichier\">";
						echo "<img src=\"./images/action/telechargement.png\" title=\"Telecharger la conversation\"/>";
						echo "</a>";
					echo "</div>";
				echo "</div>";
				}
				?>
			</div> <!-- fin div champs -->
</fieldset>

<?php
	if (isset($_GET['numpage']) && isset($_GET['archive'])) {
		echo "<ul id=\"choix\">";
			if ($_GET['numpage'] == 1 || ($_GET['numpage'] > $tab['nombreDePages'] && $tab['nombreDePages'] == 1))
				echo "<li class=\"page disabled\"> &lsaquo; Pre </li>";
			else {
				$nb = $_GET['numpage'] - 1;
				echo  "<li class=\"page\"><a href=\"index.php?page=archive&numpage=$nb\"> &lsaquo; Pre </a></li>";
			}
			for ($i=1 ; $i <= $tab['nombreDePages'] ; $i++) {
				if ($_GET['numpage'] == $i || $_GET['numpage'] > $tab['nombreDePages'])
					echo "<li class=\"num_page\" id=\"current_page\">
							<a href=\"index.php?page=archive&numpage=$i\"> $i </a>
						  </li>";
				else
					echo "<li class=\"num_page\">
						<a href=\"index.php?page=archive&numpage=$i\"> $i </a>
					  </li>";
			}
			if ($_GET['numpage'] >= $tab['nombreDePages'])
				echo "<li class=\"page disabled\"> Suiv &rsaquo; </li>";
			else {
				$nb = $_GET['numpage'] + 1;
				echo  "<li class=\"page\"><a href=\"index.php?page=archive&numpage=$nb\"> Suiv &rsaquo; </a></li>";
			}
       echo  "</ul>";
	}
	else if (isset($_GET['numpage'])) {
		echo "<ul id=\"choix\">";
			if ($_GET['numpage'] == 1 || ($_GET['numpage'] > $tab['nombreDePages'] && $tab['nombreDePages'] == 1))
				echo "<li class=\"page disabled\"> &lsaquo; Pre </li>";
			else {
				$nb = $_GET['numpage'] - 1;
				echo  "<li class=\"page\"><a href=\"index.php?page=forum&numpage=$nb\"> &lsaquo; Pre </a></li>";
			}
			for ($i=1 ; $i <= $tab['nombreDePages'] ; $i++) {
				if ($_GET['numpage'] == $i || $_GET['numpage'] > $tab['nombreDePages'])
					echo "<li class=\"num_page\" id=\"current_page\">
							<a href=\"index.php?page=forum&numpage=$i\"> $i </a>
						  </li>";
				else
					echo "<li class=\"num_page\">
						<a href=\"index.php?page=forum&numpage=$i\"> $i </a>
					  </li>";
			}
			if ($_GET['numpage'] >= $tab['nombreDePages'])
				echo "<li class=\"page disabled\"> Suiv &rsaquo; </li>";
			else {
				$nb = $_GET['numpage'] + 1;
				echo  "<li class=\"page\"><a href=\"index.php?page=forum&numpage=$nb\"> Suiv &rsaquo; </a></li>";
			}
       echo  "</ul>";
	}
?>
